Namespace Validaciones
    Public Class validacionDocumentos
        Private Shared Function ValidarNitGuion(ByVal Nit As String) As Boolean
            'fusion para validar NIT
            Dim nit1 As String
            Dim nit2 As String
            Dim nit3 As String
            Dim nit4 As String
            Dim i As Byte
            Dim tamano As Byte
            Dim caracter As String
            Dim cuenta As Byte
            Dim total As String, frontera As Integer
            Dim suma As Integer
            Dim peso As Integer
            Dim digito As Byte
            Dim aux As Single


            If Len(Nit) > 17 Then
                ValidarNitGuion = False
                Exit Function
            Else
                tamano = CByte(Nit.Length)
            End If


            If Mid(Nit, 5, 1) <> "-" Or Mid(Nit, 12, 1) <> "-" Or Mid(Nit, 16, 1) <> "-" Then
                ValidarNitGuion = False
                Exit Function
            End If

            If IsNumeric(Mid(Nit, 1, 4)) = False Or IsNumeric(Mid(Nit, 6, 6)) = False Or IsNumeric(Mid(Nit, 13, 3)) = False Or IsNumeric(Mid(Nit, 17, 1)) = False Then
                ValidarNitGuion = False
                Exit Function
            End If

            nit1 = Mid(Nit, 1, 4)
            nit2 = Mid(Nit, 6, 6)
            nit3 = Mid(Nit, 13, 3)
            nit4 = Mid(Nit, 17, 1)

            total = nit1 & nit2 & nit3 & nit4
            frontera = CInt(nit3)

            If frontera < 100 Then
                For i = 1 To 13
                    'peso = CInt(Mid(total, i, 1)) * (15 - i)
                    peso = CInt(Mid(total, i, 1)) * (15 - i)
                    suma = suma + peso
                Next
                digito = CByte(suma Mod 11)
                If digito = 10 Then digito = 0
            Else
                For i = 1 To 13
                    aux = CSng((i + 4) / 6)
                    aux = Int(aux)
                    aux = (3 + (6 * aux)) - i
                    peso = CInt(Val(Mid(total, i, 1)) * aux)
                    suma = suma + peso
                Next
                digito = CByte(suma Mod 11)
                If digito > 1 Then
                    digito = CByte(11 - digito)
                Else
                    digito = 0
                End If

            End If

            If digito <> Val(nit4) Then
                ValidarNitGuion = False
            Else
                ValidarNitGuion = True
            End If


        End Function


        Private Shared Function ValidarNitNoGuion(ByVal Nit As String) As Boolean
            'fusion para validar NIT
            Dim nit1 As String
            Dim nit2 As String
            Dim nit3 As String
            Dim nit4 As String
            Dim i As Byte
            Dim tamano As Byte
            Dim caracter As String
            Dim cuenta As Byte
            Dim total As String, frontera As Integer
            Dim suma As Integer
            Dim peso As Integer
            Dim digito As Byte
            Dim aux As Single


            If Len(Nit) <> 14 Then
                ValidarNitNoGuion = False
                Exit Function
            Else
                tamano = CByte(Nit.Length)
            End If

            If IsNumeric(Nit) = False Then
                ValidarNitNoGuion = False
                Exit Function
            End If

            nit1 = Mid(Nit, 1, 4)
            nit2 = Mid(Nit, 5, 6)
            nit3 = Mid(Nit, 11, 3)
            nit4 = Mid(Nit, 14, 1)

            total = nit1 & nit2 & nit3 & nit4
            frontera = CInt(nit3)

            If frontera < 100 Then
                For i = 1 To 13
                    peso = CByte(Mid(total, i, 1)) * (15 - i)
                    suma = suma + peso
                Next
                digito = CByte(suma Mod 11)
                If digito = 10 Then digito = 0
            Else
                For i = 1 To 13
                    aux = (CSng(i + 4) / 6)
                    aux = Int(aux)
                    aux = (3 + (6 * aux)) - i
                    peso = CInt(CInt(Mid(total, i, 1)) * aux)
                    suma = suma + peso
                Next
                digito = CByte(suma Mod 11)
                If digito > 1 Then
                    digito = CByte(11 - digito)
                Else
                    digito = 0
                End If

            End If

            If digito <> Val(nit4) Then
                ValidarNitNoGuion = False
            Else
                ValidarNitNoGuion = True
            End If


        End Function
        Public Shared Function ValidarNit(ByVal Nit As String) As Boolean

            If ValidarNitGuion(Nit) = True Or ValidarNitNoGuion(Nit) = True Then
                ValidarNit = True
            Else
                ValidarNit = False

            End If

        End Function
        Private Shared Function validarDUIInterno(ByVal dui As String) As Boolean
            Dim digitoVer As Integer
            If dui.Length <> 9 Then Return False
            If IsNumeric(dui.Substring(dui.Length - 1, 1)) = False Then
                Return False
            Else
                digitoVer = CInt(dui.Substring(dui.Length - 1, 1))
            End If
            Dim cuerpo As String = dui.Substring(0, dui.Length - 1)
            Dim s As String
            Dim i As Integer
            Dim k As Integer
            Dim suma As Integer
            For i = 9 To 2 Step -1

                s = cuerpo.Substring(k, 1)
                If IsNumeric(s) = False Then Return False
                suma += CInt(s) * i
                k += 1
            Next
            Dim resto As Integer
            resto = suma Mod 10
            Dim digito1 As Integer
            If resto = 0 Then
                digito1 = 0
            Else
                digito1 = 10 - resto
            End If
            If digitoVer <> digito1 Then
                Return False
            Else
                Return True
            End If
        End Function
        Public Shared Function validarDUI(ByVal dui As String) As Boolean
            Dim aux As String
            If dui.Length = 10 Then
                Return validarDUIInterno(dui.Substring(0, 8) & dui.Substring(9, 1))
            ElseIf dui.Length = 9 Then
                Return validarDUIInterno(dui)
            Else
                Return False
            End If
        End Function
    End Class
End Namespace
